package com.mbcn.dao.impl;

import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.mbcn.dao.MemberDAO;
import com.mbcn.model.Member;
import com.mbcn.system.commonUtil.Util;
import com.mbcn.system.hibernateUtil.HibernateUtil;

public class MemberDAOImpl implements MemberDAO {
	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;
	HttpServletRequest request = ServletActionContext.getRequest();

	public void createMember(Member member) {
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.save(member);
			transaction.commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public Member checkEmailExists(String email) {
		boolean status= true;// NOT Exists
		Member member = null;
		//HibernateUtil.getSessionFactory().openSession();
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		String hql = "FROM Member M WHERE M.email = :email";
        Query query = session.createQuery(hql);
        query.setParameter("email",email);
        member = (Member) query.uniqueResult();
		//items = session.createQuery("from MbcnItems where MbcnItems=:Category").setParameter("Category", category).list();
		transaction.commit();
		session.close();
		// TODO Auto-generated method stub
		return member;
	}

	public Member loadInfo(Integer memberId) {
		Member member = new Member();
		try {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		String hql = "FROM Member M WHERE M.memberId = :memberId";
        Query query = session.createQuery(hql);
        query.setParameter("memberId",memberId);
        member = (Member) query.uniqueResult();
		transaction.commit();
		session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return member;
	}

	public Member updateInfo(Member member) {
		try {
		Member memberOdl = new Member();
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		String hql = "FROM Member M WHERE M.email = :email";
        Query query = session.createQuery(hql);
        query.setParameter("email",member.getEmail());
        memberOdl = (Member) query.uniqueResult();
        if(memberOdl.getMemberAvt()==null && member.getMemberAvt() != null){
        	String realPath = request.getSession().getServletContext()
        			.getRealPath("/");
                	Util.copyfile(realPath +"imagesUserUploaded_tmp/"+member.getMemberAvt(), realPath+ "imagesUserUploaded/"+ member.getMemberAvt());
        } else if (!memberOdl.getMemberAvt().equals(member.getMemberAvt())){
        	String realPath = request.getSession().getServletContext()
			.getRealPath("/");
        	Util.copyfile(realPath +"imagesUserUploaded_tmp/"+member.getMemberAvt(), realPath+ "imagesUserUploaded/"+ member.getMemberAvt());
        }
        
        memberOdl.setMemberNm(member.getMemberNm());
        memberOdl.setBirthDay(member.getBirthDay());
        memberOdl.setGender(member.getGender());
        memberOdl.setMemberAvt(member.getMemberAvt());
        memberOdl.setReceiveEmail(member.getReceiveEmail());
        session.saveOrUpdate(memberOdl);
		transaction.commit();
		session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return member;
	}

	public Member changePassword(String oldPassword, String password,
			String passwordRetype) {
		// TODO Auto-generated method stub
		return null;
	}

}
